#!/usr/bin
# repertoire courant
HOME=$PWD
BUILD_DIR=$HOME'/Build'
SERVER_DIR=$1
SERVER_USERS_LIBS=${SERVER_DIR}'/repository/bundles/usr/'

if [ -z $SERVER_DIR ]
then
  echo "USAGE !! sh build.sh SERVER_DIRECTORY"
  exit
fi

if ! [ -e $BUILD_DIR ]
then
  mkdir $BUILD_DIR
fi



## Declaration des repertoires cibles a creer
LDAP_BUILD=${BUILD_DIR}'/Ldap'
MAILING_BUILD=${BUILD_DIR}'/MailingList'
SVN_BUILD=${BUILD_DIR}'/Svn'
WEB_BUILD=${BUILD_DIR}'/Web'
FORGE_BUILD=${BUILD_DIR}'/Forge'

if [ -e $MAILING_BUILD ]
then
  rm -Rf $MAILING_BUILD
fi

if [ -e $LDAP_BUILD ]
then
  rm -Rf $LDAP_BUILD
fi

if [ -e $SVN_BUILD ]
then
  rm -Rf $SVN_BUILD
fi

if [ -e $WEB_BUILD ]
then
  rm -Rf $WEB_BUILD
fi

if [ -e $FORGE_BUILD ]
then
  rm -Rf $FORGE_BUILD
fi

## Creation des repertoires cibles
echo "*************** CREATING BUILD DIRECTORY ***************"
mkdir $MAILING_BUILD
mkdir $LDAP_BUILD
mkdir $SVN_BUILD
mkdir $WEB_BUILD
mkdir $FORGE_BUILD
echo "*************** BUILD DIRECTORY CREATED ***************"

# build Ldap
echo "*************** BUILDING LDAP ***************"
LDAP_SRC="$HOME/Source/Ldap"
LDAP_SRC_JAVA="$LDAP_SRC/org/springforge/ldap/user/User.java $LDAP_SRC/org/springforge/ldap/service/LdapService.java $LDAP_SRC/org/springforge/ldap/service/impl/LdapServiceImpl.java $LDAP_SRC/org/springforge/ldap/service/impl/connexion/jndiManagerConnection.java"
cd $LDAP_BUILD
javac -classpath ${SERVER_DIR}'/repository/bundles/usr/com.springsource.org.apache.commons.codec-1.4.0.jar:' $LDAP_SRC_JAVA -d ./
cp -R $LDAP_SRC'/META-INF' ./
find . -name ".svn" | xargs rm -rf
jar cmf META-INF/MANIFEST.MF ldap.jar META-INF/ org/
cd $HOME

## build MailingList
echo "*************** BUILDING MailingList ***************"
MAILING_SRC="$HOME/Source/MailingList"
MAILING_SRC_JAVA="$MAILING_SRC/org/springforge/forgemailinglist/ForgeMailingList.java $MAILING_SRC/org/springforge/forgemailinglist/impl/ForgeMailingListImpl.java"
cd $MAILING_BUILD
javac -classpath ${SERVER_DIR}'/repository/bundles/usr/com.springsource.javax.mail-1.4.0.jar:' $MAILING_SRC_JAVA -d ./
cp -R $MAILING_SRC'/META-INF' ./
find . -name ".svn" | xargs rm -rf
jar cmf META-INF/MANIFEST.MF mailinglist.jar META-INF/ org/
cd $HOME

## build Svn
echo "*************** BUILDING Svn ***************"
SVN_SRC="$HOME/Source/Svn"
SVN_SRC_JAVA="$SVN_SRC/org/springforge/forgesvn/SVNService.java $SVN_SRC/org/springforge/forgesvn/impl/SVNServiceImpl.java"
cd $SVN_BUILD
javac -classpath ${SERVER_DIR}'/repository/bundles/usr/com.springsource.org.tmatesoft.svn-1.3.1.6109.jar' $SVN_SRC_JAVA -d ./
cp -R $SVN_SRC'/META-INF' ./
find . -name ".svn" | xargs rm -rf
jar cmf META-INF/MANIFEST.MF svn.jar META-INF/ org/
cd $HOME

## build Web
echo "*************** BUILDING Web ***************"
WEB_SRC="$HOME/Source/Web"
WEB_SRC_JAVA="$WEB_SRC/org/springforge/forgeweb/mvc/*.java"
LIBS_SPRING_DIR="$WEB_SRC/MODULE-INF/WEB-INF/lib"
LIST_LIBS_SPRING="${LIBS_SPRING_DIR}/commons-dbcp.jar:${LIBS_SPRING_DIR}/commons-logging.jar:${LIBS_SPRING_DIR}/commons-pool.jar:${LIBS_SPRING_DIR}/jstl.jar:${LIBS_SPRING_DIR}log4j-1.2.15.jar:${LIBS_SPRING_DIR}/spring.jar:${LIBS_SPRING_DIR}/spring-web.jar:${LIBS_SPRING_DIR}/spring-webmvc.jar:${LIBS_SPRING_DIR}/standard.jar:"
BUNDLES_DEP="$LDAP_BUILD/ldap.jar:$MAILING_BUILD/mailinglist.jar:$SVN_BUILD/svn.jar:${SERVER_USERS_LIBS}com.springsource.org.tmatesoft.svn-1.3.1.6109.jar:${SERVER_USERS_LIBS}com.springsource.javax.mail-1.4.0.jar:${SERVER_USERS_LIBS}com.springsource.javax.servlet-2.5.0.jar:"
cd $WEB_BUILD
javac -classpath ${LIST_LIBS_SPRING}${BUNDLES_DEP} $WEB_SRC_JAVA -d ./
cp -R $WEB_SRC/META-INF $WEB_SRC/MODULE-INF ./
find . -name ".svn" | xargs rm -rf
jar cmf META-INF/MANIFEST.MF web.jar META-INF/ org/ MODULE-INF/
cd $HOME

## create the par file
echo "*************** CREATING THE APPLICATION ARCHIVE ****************"
FORGE_SRC="$HOME/Source/Forge"
cd $FORGE_BUILD
cp -R $FORGE_SRC'/META-INF' ./
find . -name ".svn*" | xargs rm -rf
cp $LDAP_BUILD'/ldap.jar' $MAILING_BUILD'/mailinglist.jar' $SVN_BUILD'/svn.jar' $WEB_BUILD'/web.jar' ./
jar cmf META-INF/MANIFEST.MF forge.par META-INF/ ldap.jar mailinglist.jar svn.jar web.jar
cp forge.par ${SERVER_DIR}'/pickup'
cd $HOME
